﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.CF.BaseFrame.Util;
using WoChu.RfClient.Common;

namespace WoChu.RfClient.Duty
{
    public partial class FrmSortLocation : Form
    {
        public FrmSortLocation()
        {
            InitializeComponent();
        }
        private static List<GoodsInfo> _lstGoodsInfo = null;
        public static List<GoodsInfo> lstGoodsInfo
        {
            get
            {
                if (_lstGoodsInfo == null) _lstGoodsInfo = new List<GoodsInfo>();
                return _lstGoodsInfo;
            }
            set { _lstGoodsInfo = value; }
        }

        

        private void btnOK_Click(object sender, EventArgs e)
        {
            QueryData();
        }
 

        

        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter || string.IsNullOrEmpty(txtGoodsCode.Text)) return;

            QueryData();
 
        }

        private void QueryData()
        {
            string goodsCode = txtGoodsCode.Text.Trim();

           
            //设置已扫描的为加亮 

            if (goodsCode.Length >= 17)
                goodsCode = goodsCode.Substring(0, goodsCode.Length - 8);
            int curIndex = lstGoodsInfo.FindIndex(x => x.GOODSCODE == goodsCode || x.BARCODE == goodsCode);
            if (curIndex >= 0)
                this.dbGridGoods.Select(curIndex);
            else
            {
                //GoodsInfo _GoodsInfo = null;
                var result = Duty.ProdOnShelvesUIRule.NewInstance.GetPickLoction(goodsCode).DATA;

                if (result!=null)
                {
                    lstGoodsInfo.Clear();
                    lstGoodsInfo.Add(result);
                    this.GoodsSource.DataSource = null;
                    this.GoodsSource.DataSource = lstGoodsInfo;
                    this.dbGridGoods.DataSource = this.GoodsSource;
                    this.dbGridGoods.Refresh();
                }
                else
                {
                    lstGoodsInfo.Clear();
                    this.GoodsSource.DataSource = null; 
                    this.dbGridGoods.DataSource = this.GoodsSource;
                    this.dbGridGoods.Refresh();
                    //MessageBox.Show("请扫描正确的条码！");  
                }
            }
            //txtGoodsCode.Text = string.Empty;
            txtGoodsCode.SelectAll();
          
        }
 
        private void dbGridProductOnShelves_DoubleClick(object sender, EventArgs e)
        {
            //int curIndex = dbGridSortLocation.CurrentRowIndex;
            //if (curIndex < 0) return;
            //try
            //{
            //    string goodsCode = dbGridSortLocation[curIndex, 5].ToString();
            //    GoodsBoxes curItem = lstGoodsInfo.Find(x => x.GOODSCODE == goodsCode);
            //    //string ltCode = curItem.LOCATIONCODE;
            //    string gbName = curItem.GOODSNAME;

            //    //string Message = string.Format("货位:{0}, 名称:{1}, 商品批次号:{2} \n 是否删除该数据？", ltCode, gbName, gbCode);
            //    if (MessageBoxEx.Question("商品：" + gbName + " 是否删除该数据？") == DialogResult.Yes)
            //    {
            //        lstGoodsInfo.Remove(curItem);

            //        this.GoodsSource.DataSource = null;
            //        this.GoodsSource.DataSource = lstGoodsInfo;
            //        this.dbGridSortLocation.DataSource = this.GoodsSource;
            //        this.dbGridSortLocation.Refresh();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    TraceEx.Write(ex.Message);
            //    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            //}
            //finally
            //{
            //    txtBoxCode.Focus();
            //    txtBoxCode.SelectAll();
            //}
        }

        private void FrmProductOnShelves_Closed(object sender, EventArgs e)
        {
             lstGoodsInfo = null;
        }

        private void dbGridProductOnShelves_Click(object sender, EventArgs e)
        {

            int curIndex = dbGridGoods.CurrentRowIndex;
            if (curIndex < 0) return;
          txtGoodsCode.Text = dbGridGoods[curIndex, 3].ToString();
     
          this.dbGridGoods.Select(curIndex);
        }

        private void FrmSortLocation_Load(object sender, EventArgs e)
        {
            this.txtGoodsCode.Focus();
        }

    
    }
}